<html>
<head>
	<title>CS633 Group Project :: Team 1 :: Address Book</title>
	<link rel="stylesheet" href="css/style.css" type="text/css">
	<script src="js/prototype.js" type="text/javascript"></script>
	<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
	<script src="js/jquery.maskedinput-1.2.2.min.js" type="text/javascript"></script>
	<script src="js/functions.js" type="text/javascript"></script>
	
	<script type="text/javascript">
		jQuery(function($)
		{
		   	$("#phone").mask("(999) 999-9999", {placeholder:" "});
		   	$("#state").mask("aa", {placeholder:" "});
		   	$("#zip").mask("99999", {placeholder:" "});
		});
		
		function validate()
		{
			var frm = window.document.forms[0];
			
			/* Last name */
			if(frm.lname.value.trim().length == 0)
			{
				alert('Please provide the last name.');
				frm.lname.focus();
				return false;
			}
			else
			{
				if(!isName(frm.lname.value.trim()))
				{
					alert('The last name should contain only alphabetical and space characters.');
					frm.lname.focus();
					frm.lname.select();
					return false;
				}
			}
			
			/* First name */
			if(frm.fname.value.trim().length == 0)
			{
				alert('Please provide the first name.');
				frm.fname.focus();
				return false;
			}
			else
			{
				if(!isName(frm.fname.value.trim()))
				{
					alert('The first name should contain only alphabetical and space characters.');
					frm.fname.focus();
					frm.fname.select();
					return false;
				}
			}
			
			/* Street address */
			if(frm.address.value.trim().length == 0)
			{
				alert('Please provide the address.');
				frm.address.focus();
				return false;
			}
			else
			{
				if(!isStreetAddress(frm.address.value.trim()))
				{
					alert('The street address should contain only alphanumeric, hyphen, underscore, period, #, and space characters.');
					frm.address.focus();
					frm.address.select();
					return false;
				}
			}

			/* City */			
			if(frm.city.value.trim().length == 0)
			{
				alert('Please provide the city.');
				frm.city.focus();
				return false;
			}
			else
			{
				if(!isStreetAddress(frm.city.value.trim()))
				{
					alert('The city should contain only alphanumeric, hyphen, underscore, period, #, and space characters.');
					frm.city.focus();
					frm.city.select();
					return false;
				}
			}

			/* State */
			if(frm.state.value.trim().length != 2)
			{
				alert('Please provide the state.');
				frm.state.focus();
				return false;
			}
			else
			{
				if(!isStateCode(frm.state.value.trim()))
				{
					alert('The state should contain a valid US State code. Example: NY');
					frm.state.focus();
					frm.state.select();
					return false;
				}
			}
			
			/* Zip Code */
			if(frm.zip.value.trim().length != 5)
			{
				alert('Please provide the zip code.');
				frm.zip.focus();
				return false;
			}
			
			/* Phone number */
			if(frm.phone.value.trim().length < 14)
			{
				alert('Please provide a valid phone number.');
				frm.phone.focus();
				frm.phone.select();
				return false;
			}
			
			/* E-mail */
			if(!isEmail(frm.email.value.trim()))
			{
				alert('Please provide a valid e-mail address.');
				frm.email.focus();
				return false;
			}
			else
			{
				if(!isEmailAddress(frm.email.value.trim()))
				{
					alert('The e-mail should contain only alphanumeric, hyphen, underscore, period, #, @, and space characters.');
					frm.email.focus();
					frm.email.select();
					return false;
				}
			}
			
			return true;
		}	

		function confirmDelete()
		{
			msg = "Are you sure you want to remove this entry?";
			return confirm(msg);
		}
	</script>
</head>
<body>
	<%
		entryID = Request("ID")
	
		'CONNECT TO DATABASE
		
			dbPath = "db/contacts.mdb"
		
			Set conn = Server.CreateObject("ADODB.Connection")
			MdbFilePath = Server.MapPath(dbPath)
			conn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
		
		'GET FORM VALUES
			SQL_stmt = "SELECT * FROM People WHERE (PersonNo=" & entryID & ")"
			set RS = conn.execute(SQL_stmt)

		'GET FIELDS
			lname = RS("LastName")
			fname = RS("FirstName")
			address = RS("StreetAddress")
			city = RS("City")
			state = RS("State")
			zip = RS("ZipCode")
			phone = RS("PhoneNo")
			email = RS("EmailAddress")

		'CLOSE DATABASE CONNECTION
			conn.close
			set conn = nothing
			set sqlstmt = nothing
	%>
	<table width="100%">
		<!-- HEADER -->
		<tr style="padding:0px">
			<td><!--#include file ="header.asp"--></td>
		</tr>
		
		<!-- BODY -->
		<tr>
			<td align="center">
				<form name="NewEntry" method="post" action="action_edit.asp">
				<table bgcolor=#990000 width=1000 cellpadding=5 cellspacing=1 border=0>
					<tr class="header">
						<td colspan=2>Edit Contact
					<tr class="oddrow">
						<td width=200>Last Name
						<td><input id="lname" type="text" name="lname" size=25 maxlength="25" value="<%= lname %>" >
					<tr class="evenrow">
						<td width=200>First Name
						<td><input id="fname" type="text" name="fname" size=25 maxlength="25" value="<%=fname%>">
					<tr class="oddrow"> 
						<td width=200>Address
						<td><input id="address" type="text" name="address" size=25 maxlength="50" value="<%=address%>">
					<tr class="evenrow">
						<td width=200>City
						<td><input id="city" type="text" name="city" size=25 maxlength="26" value="<%=city%>">
					<tr class="oddrow">
						<td width=200>State
						<td><input id="state" type="text" name="state" size=25 value="<%=state%>" maxlength="2" onblur="this.value = this.value.toUpperCase();">
					<tr class="evenrow">
						<td width=200>Zip
						<td><input id="zip" type="text" name="zip" size=5 maxlength="5" value="<%=zip%>">
					<tr class="oddrow">
						<td width=200>Phone
						<td><input id="phone" type="text" name="phone" size=20 value="<%=phone%>">
					<tr class="evenrow">
						<td width=200>E-mail
						<td><input id="email" type="text" name="email" size=25 maxlength="50" value="<%=email%>">
						<input id="entryID" type="hidden" name="entryID" size=25 value="<%=entryID%>">
					<tr class="oddrow">
						<td width=200>
						<td><input type="button" onclick="history.go(-1);" value="&lt&lt Back" />&nbsp;&nbsp;
						<input type="button" onclick="if(confirmDelete()) { location.href='action_delete.asp?ID=<%=entryID%>'; }" value="Delete Entry">&nbsp;&nbsp;		<input type="reset" value="Reset">
						<input type="submit" onclick="return validate();" value="Change Entry">&nbsp;&nbsp;
				</table>
				</form>
			</td>
		</tr>
		
		<!-- FOOTER -->
		<tr style="padding:0px">
			<td><!--#include file ="footer.asp"--></td>
		</tr>			
	</table>
</body>